$(function(){
	$('.delete').click(function(e){
		e.preventDefault();
		var x = e.pageX;
		var y = e.pageY;
		var href = $(this).attr('href');
		$('#delete_block').css({
			'left':x + 'px',
			'top':y + 'px'
		});
		$('#delete_block .delete_yes').attr('href',href);

		$('#delete_block').show(300);
	});
	$('.delete_no').click(function(){
		e.preventDefault();
		$('#delete_block').hide(300);
	});
	$('#close_float_tags_cloud').click(function(){
		$('#float_tags_cloud').hide(400);
	});
	$('.link_cloud').click(function(e){
		var e = e || window.event;
		var posX = e.pageX;
		var posY = e.pageY;
		e.preventDefault()
		//*****addind spinner before ajax query*****
		var imgSrc ='/images/spinner_icon.gif';
		$('<img src="'+imgSrc+'">').appendTo($(this));
		var link_cloud = this;
		$(this).parent('#float_tags_cloud').hide(400);
		$('#float_tags_cloud').children().not('#close_float_tags_cloud').remove();
		var href = $(this).attr('href');
		$.ajax({
			type: "POST",
			url: href,
			error: function(){
				alert('server error! :(');
			},
			success: function(data){
				//*****removing spinner in case succes of ajax query*****
				$(link_cloud).find('img').remove();
				//$('$this').find('#cloud').show(400);
				//for editing
				if(data.clouds){
					var linkID = data['link_id'];
					var tagID = data['tag_id'];
					var max = 30;
					var min = 10;
					if(data.clouds.length == 0){
						$('#float_tags_cloud').append('<h3>There is no tags!</h3>');
						$('#float_tags_cloud').css({
							'left':posX+'px',
							'top' :posY+'px'
						}).show(400);
						return false;				
					}
					
					
					for(var i = 0; i < data.clouds.length; i++){	
						var href = '/archive/index/link_id/' + linkID;
						href += '/tag_id/' + data.clouds[i]['tag_id'];					
						var size = ((max - data.clouds[i]['general_number']) < min) ? max : parseInt(min) + parseInt(data.clouds[i]['general_number']);
						var archor = '<a style="font-size:'+size+'px" href='+href+'>'+data.clouds[i]['tag']+'('+data.clouds[i]['general_number']+')'+'</a>';
						$('#float_tags_cloud').append('<span class="action_block">'+archor+'</span>');						
					}		
				}
				$('#float_tags_cloud').css({
					'left':posX+'px',
					'top' :posY+'px'
				}).show(400);
				
			}
		});
	});
});
